package com.jishuo.nontax.api.socket.handle;

import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.ErrorCodeEnum;
import com.jishuo.base.util.SpringContextUtil;
import com.jishuo.nontax.api.socket.ApiHandle;
import com.jishuo.nontax.bfc.service.EticketNumService;
import org.dom4j.Element;

/**
 * 电子票接口处理
 *
 * @author jhb
 */
public class EticketNumApi implements ApiHandle {
    @Override
    public BaseResult execute(String head, Element body) {
        if (head.equals("getEticketNum")) { // 获取票据
            String billNum = body.elementText("billnum");
            if (billNum == null || billNum.length() == 0) return new BaseResult(ErrorCodeEnum.PARAM_ERR.getCode(), "参数错误[billnum]");

            EticketNumService eticketNumService = (EticketNumService) SpringContextUtil.getBean("eticketNumService");
            BaseResult result = eticketNumService.getEticketNum(billNum);
            //返回数据对象
            if (result.isSuccess()) {
                result.setData(((String)result.getData()).replaceAll("&quot;", "\""));
            }
            return result;
        } else if (head.equals("getEticketNumOnLine")) { //  电子缴款缴费
            String billNum = body.elementText("billnum");
            if (billNum == null || billNum.length() == 0) return new BaseResult(ErrorCodeEnum.PARAM_ERR.getCode(), "参数错误[billnum]");

            EticketNumService eticketNumService = (EticketNumService) SpringContextUtil.getBean("eticketNumService");
            BaseResult result = eticketNumService.getNewEticketNum(billNum);
            //返回数据对象
            if (result.isSuccess()) {
                result.setData(("<retCode>0000</retCode><retMsg>success</retMsg>"+result.getData()).replaceAll("&quot;", "\""));
            }else{
                result.setData(("<retCode>9999</retCode><retMsg>fail</retMsg>"+result.getData()).replaceAll("&quot;", "\""));

            }
            return result;
        } else if (head.equals("setEticketNumDeposit")) { //  电子缴款缴费
            String billnum = body.elementText("billnum");
            String paybankdt = body.elementText("paybankdt");
            String bankaffirm = body.elementText("bankaffirm");
            String bankstation = body.elementText("bankstation");
            String paychnl = "00"; // body.elementText("paychnl");
            String payeraccount = ""; // body.elementText("payeraccount");
            int paywayid = 0;
            try {
                paywayid = Integer.parseInt(body.elementText("paywayid"));
            } catch(Exception e) {
                return new BaseResult(ErrorCodeEnum.PARAM_ERR.getCode(), "参数错误[paywayid]");
            }
            EticketNumService eticketNumService = (EticketNumService) SpringContextUtil.getBean("eticketNumService");
            BaseResult ret = eticketNumService.setEticketNumDeposit(billnum, paybankdt, bankaffirm, bankstation, paywayid,paychnl,payeraccount, null);
            int n = (ret.isSuccess()) ? 1 : 0;
            ret.setData("<returninfo>" + n + "</returninfo>");
            return ret;
        }else if (head.equals("setEticketNumDepositOnLine")) {
            String billnum = body.elementText("billnum");
            String paybankdt = body.elementText("paybankdt");
            String bankaffirm = body.elementText("bankaffirm");
            String paychnl = body.elementText("paychnl");
            String payeraccount = body.elementText("payeraccount");
            int paywayid = 3;
            EticketNumService eticketNumService = (EticketNumService) SpringContextUtil.getBean("eticketNumService");
            BaseResult result = eticketNumService.setEticketNumDeposit(billnum, paybankdt, bankaffirm, "online", paywayid,paychnl,payeraccount, null);
            //返回数据对象
            if (result.isSuccess()) {
                result.setData("<retCode>0000</retCode><retMsg>success</retMsg>");
            }else{
                result.setData("<retCode>9999</retCode><retMsg>fail</retMsg>");
            }
            return result;
        }
        return null;
    }
}
